package servlet.api;

import com.alibaba.fastjson.JSON;
import entity.Category;
import service.CategoryService;
import service.impl.CategoryServiceImpl;
import utils.R;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "CategoryServlet",urlPatterns = "/api/category/*")
public class CategoryServlet extends HttpServlet {
    private final CategoryService categoryService=new CategoryServiceImpl();

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");
        response.setHeader("Access-Control-Allow-Origin","*");
        response.setHeader("Access-Control-Allow-Methods","*");
        response.setHeader("Access-Control-Allow-Headers", "*");
        String path=request.getPathInfo();
        R result;
        if ("/tree".equals(path)){
            List<Category> tree=categoryService.findTree();
            result= R.ok(tree);
        }else{
            List<Category> list=categoryService.findAll();
            result= R.ok(list);
        }
        PrintWriter out = response.getWriter();
        out.print(JSON.toJSONString(result));
        out.flush();
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html;charset=UTF-8");
        resp.setHeader("Access-Control-Allow-Origin","*");
        resp.setHeader("Access-Control-Allow-Methods","*");
        resp.setHeader("Access-Control-Allow-Headers", "*");
        BufferedReader reader = req.getReader();
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = reader.readLine()) != null) {
            sb.append(line);
        }
        Category category=JSON.parseObject(sb.toString(),Category.class);
        boolean success=categoryService.saveOrUpdate(category);
        R result=success ? R.ok():R.fail();
        PrintWriter out = resp.getWriter();
        out.print(JSON.toJSONString(result));
        out.flush();
        out.close();
    }

    @Override
    protected void doDelete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("application/json;charset=UTF-8");
        resp.setHeader("Access-Control-Allow-Origin","*");
        resp.setHeader("Access-Control-Allow-Methods","*");
        resp.setHeader("Access-Control-Allow-Headers", "*");
        String pathInfo=req.getPathInfo();
        Long id=null;
        if (pathInfo!=null && pathInfo.length()>1){
            id=Long.valueOf(pathInfo.substring(1));
        }
        boolean success=id != null && categoryService.delete(id);
        R result=success ? R.ok():R.fail();
        PrintWriter out = resp.getWriter();
        out.print(JSON.toJSONString(result));
        out.flush();
        out.close();
    }
}
